package com.dingwei.superiordiary.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.dingwei.superiordiary.R;
import com.dingwei.superiordiary.adapter.adapter.HisPostsAdapter;
import com.dingwei.superiordiary.bean.MyCareMeBean;
import com.dingwei.superiordiary.bean.MyPostsBean;
import com.dingwei.superiordiary.mvp.presenter.HisPostsPresenter;
import com.dingwei.superiordiary.mvp.presenter.MyPostsPresenter;
import com.dingwei.superiordiary.mvp.view.Contract;
import com.jude.easyrecyclerview.adapter.RecyclerArrayAdapter;
import com.jude.easyrecyclerview.decoration.DividerDecoration;
import com.lihao.baselibrary.base.BaseActivity;
import com.lihao.baselibrary.utils.DisplayUtil;
import com.nikhilpanju.recyclerviewenhanced.RecyclerTouchListener;
import com.scwang.smartrefresh.layout.SmartRefreshLayout;
import com.scwang.smartrefresh.layout.api.RefreshLayout;
import com.scwang.smartrefresh.layout.listener.OnRefreshLoadmoreListener;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * Created by lihao on 2018/1/11.
 */

public class HisPostsActivity extends BaseActivity implements Contract.HisPostsView {

    HisPostsPresenter myPostsPresenter = new HisPostsPresenter(this);
    @Bind(R.id.img_back)
    ImageView imgBack;
    @Bind(R.id.tv_title)
    TextView tvTitle;
    @Bind(R.id.tv_right)
    TextView tvRight;
    @Bind(R.id.m_recyclerView)
    RecyclerView mRecyclerView;
    @Bind(R.id.no_data_layout)
    RelativeLayout noDataLayout;
    @Bind(R.id.refresh_layout)
    SmartRefreshLayout refreshLayout;


    private HisPostsAdapter adapter;
    private int page = 1;

    private String userid;

    @Override
    protected int getContentViewId() {
        return R.layout.activity_my_collect;
    }

    @Override
    protected void initData() {
        refreshLayout.autoRefresh();
    }

    protected void initView() {

        userid = getIntent().getStringExtra("userid");
        if (BaseActivity.value.getUserId().equals(userid)) {
            tvTitle.setText("我的帖子");
        } else {
            tvTitle.setText("TA的帖子");
        }
        LinearLayoutManager layoutManager = new LinearLayoutManager(context);
        layoutManager.setOrientation(LinearLayoutManager.VERTICAL);
        mRecyclerView.setLayoutManager(layoutManager);
        mRecyclerView.setNestedScrollingEnabled(false);
        mRecyclerView.addItemDecoration(new DividerDecoration(getResources().getColor(R.color.parting_line), DisplayUtil.dip2px(BaseActivity.context, 1)));
        adapter = new HisPostsAdapter(this);
        mRecyclerView.setAdapter(adapter);

        adapter.setOnItemClickListener(new RecyclerArrayAdapter.OnItemClickListener() {
            @Override
            public void onItemClick(int position) {
                MyPostsBean.DataBean.ListBean bean = adapter.getItem(position);
                intent = new Intent(BaseActivity.context, FindDetailsActivity.class);
                intent.putExtra("id", bean.id);
                startActivity(intent);
            }
        });

    }

    @Override
    protected void initListener() {
        refreshLayout.setOnRefreshLoadmoreListener(new OnRefreshLoadmoreListener() {
            @Override
            public void onLoadmore(RefreshLayout refreshlayout) {
                page = page + 1;
                myPostsPresenter.initData(page, userid, false, false);
            }

            @Override
            public void onRefresh(RefreshLayout refreshlayout) {
                page = 1;
                myPostsPresenter.initData(page, userid, true, false);
            }
        });
        adapter.xxInterfaceListener(new HisPostsAdapter.xxInterFace() {
            @Override
            public void attentioned(int position) {
                MyPostsBean.DataBean.ListBean bean = adapter.getItem(position);
                int action = 0;
                if (bean.attentioned==0) {
                    action = 1;
                } else {
                    action = 0;
                }
                myPostsPresenter.attention(bean.consumer_id,position,action);
            }
        });

    }


    @Override
    protected void onResume() {
        super.onResume();
    }

    @Override
    protected void onPause() {
        super.onPause();
    }

    public void showData() {
        if (adapter.getAllData() == null || adapter.getAllData().size() == 0) {
            noDataLayout.setVisibility(View.VISIBLE);
            mRecyclerView.setVisibility(View.GONE);
        } else {
            mRecyclerView.setVisibility(View.VISIBLE);
            noDataLayout.setVisibility(View.GONE);
        }
    }

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // TODO: add setContentView(...) invocation
        ButterKnife.bind(this);
    }

    @Override
    public void onRefresh(MyPostsBean bean) {
        adapter.clear();
        adapter.addAll(bean.data.list);
        refreshLayout.finishRefresh();
        showData();
    }

    @Override
    public void onLoadMore(MyPostsBean bean) {
        adapter.addAll(bean.data.list);
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void onFail() {
        refreshLayout.finishRefresh();
        refreshLayout.finishLoadmore();
        showData();
    }

    @Override
    public void attentionedSuccess(int position) {
        MyPostsBean.DataBean.ListBean listBean = adapter.getItem(position);
        if (listBean.attentioned==0) {
            listBean.attentioned = 1;
        } else {
            listBean.attentioned = 0;
        }
        adapter.notifyItemChanged(position, listBean);
    }


    @OnClick(R.id.img_back)
    public void onViewClicked() {
        finish();
    }
}
